package com.freemyleft.left.zapp.ui;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.lljjcoder.city_20170724.CityPickerView;
import com.lljjcoder.city_20170724.bean.CityBean;
import com.lljjcoder.city_20170724.bean.DistrictBean;
import com.lljjcoder.city_20170724.bean.ProvinceBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by FreeMyleft on 2017/10/12.
 */

public class PickerViewUtils {
    /**
     * @param editText 输入框
     * @param context  当前ActivityCotext
     */
    public static void showPickerCity(final AppCompatEditText editText, Context context) {
        CityPickerView cityPicker = new CityPickerView.Builder(context).textSize(20)
                .cancelTextColor("#333333")
                .confirTextColor("#333333")
                .title("")
                .backgroundPop(0xa0000000)
                .province("北京市")
                .city("南京市")
                .district("秦淮区")
                .textColor(Color.parseColor("#000000"))
                .provinceCyclic(true)
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(5)
                .itemPadding(10)
                .build();
        cityPicker.show();
        cityPicker.setOnCityItemClickListener(new CityPickerView.OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean provinceBean, CityBean cityBean, DistrictBean districtBean) {
                //省份
                String province = provinceBean.getName();
                //城市
                String city = cityBean.getName();
                //区县（如果设定了两级联动，那么该项返回空）
                String district = districtBean.getName();
                editText.setText(province + "-" + city + "-" + district);
            }

            @Override
            public void onCancel() {

            }
        });
    }

    public static void showPickerCity(final AppCompatTextView editText, Context context) {
        CityPickerView cityPicker = new CityPickerView.Builder(context).textSize(20)
                .cancelTextColor("#333333")
                .confirTextColor("#333333")
                .title("")
                .backgroundPop(0xa0000000)
                .province("北京市")
                .city("南京市")
                .district("秦淮区")
                .textColor(Color.parseColor("#000000"))
                .provinceCyclic(true)
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(5)
                .itemPadding(10)
                .build();
        cityPicker.show();
        cityPicker.setOnCityItemClickListener(new CityPickerView.OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean provinceBean, CityBean cityBean, DistrictBean districtBean) {
                //省份
                String province = provinceBean.getName();
                //城市
                String city = cityBean.getName();
                //区县（如果设定了两级联动，那么该项返回空）
                String district = districtBean.getName();
                editText.setText(province + "-" + city + "-" + district);
            }

            @Override
            public void onCancel() {

            }
        });
    }


    /**
     * @param optionItems 数据集合
     * @param editText    输入框
     * @param context     当前ActivityCotext
     */
    public static void showPickerSingle(final ArrayList<String> optionItems,
                                        final TextView editText,
                                        Context context) {
        OptionsPickerView pvOptinos = new OptionsPickerView.Builder(
                context, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String tx = optionItems.get(options1);
                editText.setText(tx);
            }
        }).setTitleText("").
                setContentTextSize(20)//设置滚轮文字大小
                .setSubmitText("确定")
                .setCancelText("取消")
                .setDividerColor(Color.LTGRAY)//设置分割线的颜色
                .setSelectOptions(0, 1)//默认选中项
                .setBgColor(Color.WHITE)
                .setTitleBgColor(Color.WHITE)
                .setTitleColor(Color.LTGRAY)
                .setCancelColor(Color.BLACK)
                .setSubmitColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK)
                .isCenterLabel(false)//是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setBackgroundId(0xa0000000)
                .build();
        pvOptinos.setPicker(optionItems);

        pvOptinos.show();
    }

    /**
     * @param optionItems 数据集合
     * @param editText    输入框
     * @param context     当前ActivityCotext
     */
    public static void showPickerSingle(final ArrayList<String> optionItems,
                                        final AppCompatTextView editText,
                                        Context context) {
        OptionsPickerView pvOptinos = new OptionsPickerView.Builder(
                context, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String tx = optionItems.get(options1);
                editText.setText(tx);
            }
        }).setTitleText("").
                setContentTextSize(20)//设置滚轮文字大小
                .setSubmitText("确定")
                .setCancelText("取消")
                .setDividerColor(Color.LTGRAY)//设置分割线的颜色
                .setSelectOptions(0, 1)//默认选中项
                .setBgColor(Color.WHITE)
                .setTitleBgColor(Color.WHITE)
                .setTitleColor(Color.LTGRAY)
                .setCancelColor(Color.BLACK)
                .setSubmitColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK)
                .isCenterLabel(false)//是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setBackgroundId(0xa0000000)
                .build();
        pvOptinos.setPicker(optionItems);

        pvOptinos.show();
    }

    public static void showDetePickerView(final TextView editText,
                                          Context context) {
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        //endDate.set(2020,1,1);
        //正确设置方式 原因：注意事项有说明
        long current=System.currentTimeMillis();
        startDate.setTimeInMillis(current);
        endDate.setTimeInMillis(current+5184000000l);
        TimePickerView pvTime = new TimePickerView.Builder(context, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                editText.setText(DateToStr(date));
            }
        })
                .setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")//确认按钮文字
                .setContentSize(18)//滚轮文字大小
                .setTitleSize(20)//标题文字大小
                .setTitleText("请选择时间")//标题文字
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(true)//是否循环滚动
                .setTitleColor(Color.BLUE)//标题文字颜色
                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                .setCancelColor(Color.BLUE)//取消按钮文字颜色
                .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                .setBgColor(Color.WHITE)//滚轮背景颜色 Night mode
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate,endDate)//起始终止年月日设定
                .setLabel("年","月","日","时","分","秒")//默认设置为年月日时分秒
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(true)//是否显示为对话框样式
                .build();
        pvTime.show();
    }
    public static String DateToStr(Date date) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String str = format.format(date);
        return str;
    }


}
